﻿using System;
using System.Collections.Generic;
using SilveR.DataValidators;
using SilveR.Views.StatsViews;
using System.Windows;

namespace SilveR.DataValidators
{
    public class PowerAnalysisValidator : DataValidator
    {
        private readonly PowerAnalysisVariables paVariables;

        public PowerAnalysisValidator(PowerAnalysisVariables pa)
            : base(pa.SelectedDataHandler)
        {
            paVariables = pa;
        }

        public override bool Validate()
        {
            //Get the datatable
            if (DataTable != null) //sometimes not using a dataset at all...
            {
                //go through all the column names, if any are numeric then stop the analysis
                List<string> allVars = new List<string>();
                allVars.Add(paVariables.Treatment);
                allVars.Add(paVariables.Response);
                if (!CheckColumnNames(allVars)) return false;

                if (!String.IsNullOrEmpty(paVariables.Treatment))
                {
                    if(!CheckTreatmentsHaveLevels(paVariables.Treatment)) return false;
                }

                //Check that the response does not contain non-numeric 
                if (!CheckIsNumeric(paVariables.Response))
                {
                    MessageBox.Show("Error: The response variable selected contains non-numerical data that cannot be processed. Please check raw data and make sure the data was entered correctly.", "Validate Data", MessageBoxButton.OK);
                    return false;
                }

                if (!CheckResponsesPerLevel(paVariables.Treatment, paVariables.Response)) return false;

                if (!String.IsNullOrEmpty(paVariables.Treatment) && !String.IsNullOrEmpty(paVariables.Response)) //if a treat and response is selected...
                {
                    //Check that the number of responses for each level is at least 2
                    Dictionary<string, int> levelResponses = ResponsesPerLevel(paVariables.Treatment, paVariables.Response);
                    foreach (KeyValuePair<string, int> level in levelResponses)
                    {
                        if (level.Value < 2)
                        {
                            MessageBox.Show("Error: There is no replication in one or more of the levels of the treatment factor. Please select another factor.", "Validate Data", MessageBoxButton.OK);
                            return false;
                        }
                    }

                    //check response and doses contain values
                    if (!CheckResponseAndTreatmentsNotBlank(paVariables.Response, paVariables.Treatment, "treatment factor")) return false;
                }
                else if (String.IsNullOrEmpty(paVariables.Treatment) && !String.IsNullOrEmpty(paVariables.Response))
                    //if only a response selected (doing absolute change) then check that more than 1 value is in the dataset!
                {
                    if (NoOfResponses(paVariables.Response) == 1)
                    {
                        MessageBox.Show("Error: The response selected contains only 1 value. Please select another factor.", "Validate Data", MessageBoxButton.OK);
                        return false;
                    }
                }
            }

            //if get here then no errors so return true
            return true;
        }
    }
}